﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Administracija.Repository;

namespace Administracija
{
    public partial class SablonRecView : Form
    {
        RSablon rSablon = new RSablon();
        RSablonTip rSablonTip = new RSablonTip();
        int id;

        public SablonRecView(int id)
        {
            InitializeComponent();
            this.id = id;
        }

        private void btnTestSQL_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            tabControl1.SelectTab(0);

            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(String.Format(memoEdit1.Text,edtParametar.Text), Common.Properties.Settings.Default.ConnectionString);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

                // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                dataGridView1.DataSource = table;

                // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns(
                    DataGridViewAutoSizeColumnsMode.ColumnHeader);



                richTextBox1.AppendText("(" + Convert.ToString(table.Rows.Count) + " row(s) affected)" + Environment.NewLine);
                richTextBox1.AppendText("(" + Convert.ToString(table.Columns.Count) + " column(s) affected)" + Environment.NewLine);

            }
            catch (Exception ex)
            {
                tabControl1.SelectTab(1);

                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.AppendText(ex.Message + Environment.NewLine);
                richTextBox1.SelectionColor = Color.Black;
                memoEdit1.Focus();
            }

        }

        private void SablonRecView_Load(object sender, EventArgs e)
        {
            bsTipSablona.DataSource = rSablonTip.VratiSve();

            Common.MultiLanguage.Tool.PrevediFormu(this);

            if (id == 0) bindingSource1.DataSource = rSablon.Nov();
            else
                bindingSource1.DataSource = rSablon.Izmeni(id);
        }

        private void btnSnimi_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.EndEdit();
                int i = rSablon.Snimi((DataLayer.Sablon)bindingSource1.Current);
                rSablon.VratiSve().ToList().FindIndex(c => c.SablonId == i);
            }
            catch (Exception ex)
            {
                Common.Tools.MessageError.Show(ex.Message);
                this.DialogResult = DialogResult.None;
            }
        }

        private void btnTestWord_Click(object sender, EventArgs e)
        {
            try
            {
                Common.Tools.Sablon c = new Common.Tools.Sablon();
                c.Kreiraj(this.id, Convert.ToInt32(edtParametar.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
